package cn.javacc.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 
 * @author mzh2008@163.com 2012-11-13 下午10:28:49
 *
 */
@Service
public class JspViewInterceptor extends HandlerInterceptorAdapter {
	private static final String KEY_BASE = "base";
	private static final String KEY_LESS_ENABLED = "lessEnabled";

	private boolean lessEnabled;

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		request.setAttribute(KEY_BASE, this.getBaseURL(request));
		request.setAttribute(KEY_LESS_ENABLED, this.lessEnabled);
		request.setAttribute("lang", "zh");
	}

	private String getBaseURL(HttpServletRequest request) {
		StringBuilder b = new StringBuilder();
		b.append("http://");
		b.append(request.getServerName());
		b.append(":");
		b.append(request.getServerPort());
		b.append(request.getContextPath());
		b.append("/");
		return b.toString();
	}

	public boolean isLessEnabled() {
		return lessEnabled;
	}

	@Value("${lessEnabled}")
	public void setLessEnabled(boolean lessEnabled) {
		this.lessEnabled = lessEnabled;
	}

}